<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/gruppenkinder.php";
require_once $basedir."/inc/gruppen.php";
$gruppen_id = "";
$activ = false;
$mode = "all";
$gruppen = Gruppen::getAll();
if(isset($_REQUEST['gruppen_id'])) {
    $gruppen_id = $_REQUEST['gruppen_id'];
}
if(isset($_REQUEST['activ'])) {
    $activ = $_REQUEST['activ'];
}
if(isset($_REQUEST['mode'])) {
    $mode = $_REQUEST['mode'];
}
if(!isset($format)) {
    $format = "html";
}
if($format=="pdf") {
    require_once $basedir."/inc/pdf.php";
    $gruppe = new Gruppen($gruppen_id);
    $pdf = new PDF();
    $pdf->Open();
    $pdf->SetAutoPageBreak(true, 20);
    $pdf->SetMargins(20, 5, 20, 5);
    $pdf->setGruppe($gruppe);
    $pdf->AddFont("ShannonAtt",'','shannon_att.php');
    $pdf->AddPage();
    $pdf->AliasNbPages();
    $pdf->Content($gruppen_id, $mode);
    $pdf->Output();
    exit;
}
$kinderlist = GruppenKinder::getAll($activ, $gruppen_id);
?>
<script language="JavaScript" type="text/javascript">
    $(function() {
        $('#nurAktuelle').click(function(event) {
            var params = [
                'page=<?php echo $menuid; ?>',
                'activ=' + $('#nurAktuelle').is(':checked'),
                'gruppen_id=' + $('#Gruppe').val(),
                'mode=' + $('#Mode').val()
            ];
            window.location.href = "http://" + window.location.host + window.location.pathname + "?" + params.join('&');
        });
        $('#PDF').click(function(event) {
            var params = [
                'page=<?php echo $menuid; ?>',
                'activ=' + $('#nurAktuelle').is(':checked'),
                'gruppen_id=' + $('#Gruppe').val(),
                'mode=' + $('#Mode').val()
            ];
            if(window.location.pathname.match(/index\.php/)) {
                window.location.href = "http://" + window.location.host + window.location.pathname.replace("index", "pdf") + "?" + params.join('&');
            } else {
                window.location.href = "http://" + window.location.host + "/pdf.php?" + params.join('&');
            }
        });
        $('#Gruppe').change(function(event) {
            var params = [
                'page=<?php echo $menuid; ?>',
                'activ=' + $('#nurAktuelle').is(':checked'),
                'gruppen_id=' + $('#Gruppe').val(),
                'mode=' + $('#Mode').val()
            ];
            window.location.href = "http://" + window.location.host + window.location.pathname + "?" + params.join('&');
        });
        $('#Mode').change(function(event) {
            var params = [
                'page=<?php echo $menuid; ?>',
                'activ=' + $('#nurAktuelle').is(':checked'),
                'gruppen_id=' + $('#Gruppe').val(),
                'mode=' + $('#Mode').val()
            ];
            window.location.href = "http://" + window.location.host + window.location.pathname + "?" + params.join('&');
        });
    });
</script>
<div>
    <input type='checkbox' value='' name='activ' id='nurAktuelle' <?php echo $activ?'checked':''; ?>></input> Nur Aktuelle
    <select name='gruppen_id' id='Gruppe'>
        <option value=''>Alle</option>
<?php
for($i=0; $i<count($gruppen); $i++) {
    $gruppe = $gruppen[$i];
    $selected = "";
    if($gruppe->getId() == $gruppen_id) {
        $selected = " selected='selected'";
    }
?>
        <option value='<? echo $gruppe->getId(); ?>'<? echo $selected; ?>><?echo $gruppe->getName(); ?></option>
<?php
}
?>
    </select>
    <select name='mode' id='Mode'>
        <option value='all'<? echo $mode=='all'?" selected":""; ?>>Alle Daten</option>
        <option value='allow'<? echo $mode=='allow'?" selected":""; ?>>Nur Freigegebene Daten</option>
    </select>
    <button id='PDF'>PDF</button>
</div>
<table>
    <tr>
        <th>Vorname</th>
        <th>Nachname</th>
        <th>Geburtsdatum</th>
        <th>Adresse</th>
        <th>Telefon</th>
        <th>Name Mutter</th>
        <th>Mobile</th>
        <th>eMail</th>
        <th>Name Vater</th>
        <th>Modile</th>
        <th>eMail</th>
    </tr>
<?php
for($i=0; $i<sizeof($kinderlist); $i++) {
    $kind = $kinderlist[$i]->getKind();
    $kind_nachname = $kind->isNameShow()||$mode=='all'?$kind->getNachname():"";
    $kind_geburtstag = $kind->isNameShow()||$mode=='all'?$kind->getGeburtsdatum():"";
    $kind_adresse = "";
    if($kind->isAdresseShow()||$mode=='all') {
        $adresse = $kind->getAdresse();
        $kind_adresse = $adresse->getStrasse()." ".$adresse->getHausnummer()." ".$adresse->getZusatzHausnummer()."<br />".$adresse->getPlz()." ".$adresse->getOrt();
    }
    $kind_telefon = $kind->getAdresse()->isTelefonShow()||$mode=='all'?$kind->getAdresse()->getTelefon():"";
    $vater = $kind->getVater();
    $mutter = $kind->getMutter();
    $mutter_name = $mutter->isNameShow()||$mode=='all'?$mutter->getVorname()." ".$mutter->getNachname():"";
    $mutter_mobile = $mutter->isMobilShow()||$mode=='all'?$mutter->getMobil():"";
    $mutter_email = $mutter->isEMailShow()||$mode=='all'?$mutter->getEMail():"";
    $vater_name = $vater->isNameShow()||$mode=='all'?$vater->getVorname()." ".$vater->getNachname():"";
    $vater_mobile = $vater->isMobilShow()||$mode=='all'?$vater->getMobil():"";
    $vater_email = $vater->isEMailShow()||$mode=='all'?$vater->getEMail():"";
?>
    <tr>
        <td><? echo $kind->getVorname(); ?></td>
        <td><? echo $kind_nachname; ?></td>
        <td><? echo $kind_geburtstag; ?></td>
        <td><? echo $kind_adresse; ?></td>
        <td><? echo $kind_telefon; ?></td>
        <td><? echo $mutter_name; ?></td>
        <td><? echo $mutter_mobile; ?></td>
        <td><? echo $mutter_email; ?></td>
        <td><? echo $vater_name; ?></td>
        <td><? echo $vater_mobile; ?></td>
        <td><? echo $vater_email; ?></td>
    </tr>
<?php
}
?>
</table>